/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.model;

import org.gac.gacmod.genetics.ExecutionInput;
import org.gac.gacmod.genetics.ExecutionOutput;
import org.gac.api.genetics.Population;

/**
 *
 * @author Tomek
 */
@StepCat("Flow steps")
public class SplitStep extends CompositeStep
{

    public SplitStep(String name)
    {
        super(name);
    }

        public SplitStep()
    {
        this( null );
    }

    @Override
    public ExecutionOutput Run(ExecutionInput input)
    {
        if( _innerSteps.size()== 0 )
            return new ExecutionOutput(null, false, input.getPopulation());

        Population pop = input.getPopulation();
        Population resultPopulation = new Population();

        for( Step s: _innerSteps )
        {
            ExecutionOutput out = s.Run( new ExecutionInput(
                    input.getProblemDefinition(), pop));

            resultPopulation.addAll(out.getPopulation());
            if( out.isStop())
            {
                return new ExecutionOutput(out.getSolution(), true, out.getPopulation());
            }
        }

        return new ExecutionOutput(null,false, resultPopulation);
    }
}
